package com.zhj.agent.resolver;

import com.zhj.agent.support.ClassEnhancePluginDefine;

import org.springframework.util.Assert;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * 通过配置文件信息进行加载
 */
public class PropertiesLoader implements PluginLoader<List<ClassEnhancePluginDefine>> {

    private final Properties properties = new Properties();

    /**
     * 加载properties文件
     *
     * @param source source
     * @return the properties
     * @since 0.0.1
     */
    @Override
    public List<ClassEnhancePluginDefine> load(String source) {
        Assert.hasText(source, "资源路径不能为空");
        String path = Objects.requireNonNull(getClass().getClassLoader().getResource(source), "资源文件不存在").getPath();
        try {
            FileInputStream fileInputStream = new FileInputStream(path);
            this.properties.load(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("配置文件加载失败");
        }
        return process(this.properties);
    }

    private List<ClassEnhancePluginDefine> process(Properties properties) {
        return this.properties.values().stream().map(v -> {
            String v1 = (String) v;
            String[] split = v1.split(",");
        }).collect(Collectors.toList());
    }
}
